var e = require("../../utils/config"), t = require("../../utils/request"), o = require("../../utils/api"), a = require("../../utils/storage"), i = require("../../utils/self-selected-order"), n = require("../../utils/map-convert");

Component({
    properties: {
        marginTop: {
            type: String,
            value: "0px"
        }
    },
    data: {
        themeColor: getApp().globalData.themeColor,
        x: getApp().globalData.systemWidth / 2 - 30,
        btnX: getApp().globalData.systemWidth / 4 - 30,
        recommendList: [],
        recommendItemList: []
    },
    lifetimes: {
        attached: function() {
            this.getIndexRecommend(), this.getIndexRecommendItem();
        }
    },
    methods: {
        getIndexRecommend: function() {
            var a = this;
            t.request(t.HttpMethod.GET, o.apiRoute.selectWcaIndexModuleByModuleName + "?moduleName=Recommend", {}).then(function(t) {
                var o = [];
                t.success && t.data && t.data.forEach(function(t) {
                    t.url = e.resourceRoot + t.imageUrl, o.push(t);
                }), a.setData({
                    recommendList: o
                });
            });
        },
        getIndexRecommendItem: function() {
            var a = this;
            t.request(t.HttpMethod.GET, o.apiRoute.selectWcaIndexModuleByModuleName + "?moduleName=RecommendItem", {}).then(function(t) {
                var o = [];
                if (t.success && t.data) {
                    var i = 0;
                    t.data.forEach(function(t) {
                        t.url = e.resourceRoot + t.imageUrl, t.className = i % 2, i++, o.push(t);
                    });
                }
                console.log(o), a.setData({
                    recommendItemList: o
                });
            });
        },
        recommendClick: function(e) {
            var t = e.currentTarget.dataset.item;
            if (console.log(t), 0 == t.redirectType) if ("SelfSelectedOrder" == t.name) this.selfSelectedOrderTap(); else {
                wx.showLoading({
                    title: "正在加载中...",
                    mask: !0
                });
                var o = a.getStorageSync(a.storageKey.openId);
                if (t.redirectUrl = t.redirectUrl.replace("{{openId}}", o), t.redirectUrl.indexOf("{{uid}}") > -1) {
                    if (!a.isLogin()) return void wx.navigateTo({
                        url: "/pages/authorize/authorize"
                    });
                    var i = a.getStorageSync(a.storageKey.uid);
                    t.redirectUrl = t.redirectUrl.replace("{{uid}}", i);
                }
                wx.navigateTo({
                    url: "/pages/webview/webview?h5=".concat(encodeURIComponent(t.redirectUrl)),
                    success: function() {
                        wx.hideLoading();
                    }
                });
            } else 1 == t.redirectType && wx.navigateToMiniProgram({
                appId: t.wxAppId,
                path: t.wxRedirectUrl,
                extraData: {},
                envVersion: "release"
            });
        },
        selfSelectedOrderTap: function() {
            var e = this;
            if (a.isLogin()) {
                var t = a.getStorageSync(a.storageKey.cityInfo), o = a.getStorageSync(a.storageKey.locationInfo);
                null != t && null != t && "" != t && null != o && null != o && "" != o ? e.goSelfSelectedOrderIndex() : wx.getLocation({
                    type: "gcj02",
                    success: function(t) {
                        console.log("自选项目首页，微信获取的位置信息", t);
                        var o = n.qqMapToBMap(t.longitude, t.latitude);
                        a.setStorageSync(a.storageKey.locationInfo, {
                            expireTimestamp: new Date().getTime() + 6e5,
                            latitude: o[1].toString(),
                            longitude: o[0].toString()
                        }), e.selectCityByCoordinates(o[1].toString(), o[0].toString());
                    },
                    fail: function(e) {
                        console.log("微信获取位置信息失败：" + e.errMsg), e.errMsg.indexOf("频繁") > -1 ? console.log("请勿频繁定位") : wx.showModal({
                            title: "提示",
                            content: "请点击小程序右上角“···”，设置中允许使用位置信息，并重新进入小程序",
                            showCancel: !1
                        });
                    }
                });
            } else wx.navigateTo({
                url: "/pages/authorize/authorize?path=selfSelectedOrderIndex"
            });
        },
        selectCityByCoordinates: function(e, i) {
            var n = this, r = o.apiRoute.selectCityByCoordinates + "?latitude=" + e + "&longitude=" + i;
            t.request(t.HttpMethod.GET, r, {}).then(function(e) {
                if (e.success) {
                    var t = e.data.filter(function(e) {
                        return "1" == e.deep;
                    });
                    if (null != t && null != t && t.length > 0) {
                        var o = t[0].id + "00", i = t[0].name;
                        n.setData({
                            city: i,
                            cityCode: o
                        }), a.setStorageSync(a.storageKey.cityInfo, {
                            city: i,
                            cityCode: o
                        }), n.goSelfSelectedOrderIndex();
                    }
                }
            });
        },
        goSelfSelectedOrderIndex: function() {
            var t = a.getStorageSync(a.storageKey.cityInfo), o = a.getStorageSync(a.storageKey.locationInfo);
            i.getJwt().then(function(a) {
                if (a.success) {
                    var i = a.data.token, n = "".concat(e.selfSelectedOrderUrl, "check_user?token=").concat(i, "&cityCode=").concat(null == t.cityCode ? "310100" : t.cityCode);
                    null != o && null != o && "" != o && (n += "&latitude=".concat(o.latitude, "&longitude=").concat(o.longitude)), 
                    console.log("跳转线上组单首页地址：" + n), wx.navigateTo({
                        url: "/pages/webview/webview?h5=".concat(encodeURIComponent(n)),
                        success: function() {}
                    });
                }
            });
        },
        btnClick: function(t) {
            if (a.isLogin()) {
                if ("MyReport" == t.target.dataset.id && wx.switchTab({
                    url: "/pages/my-link-report/my-link-report"
                }), "MyEInvoice" == t.target.dataset.id) {
                    var o = this;
                    o.setData({
                        loading: !0
                    }), e.getH5EInvoiceUrl().then(function(e) {
                        var t = e;
                        t = t.replace("{0}", "../../pages/my/my"), wx.navigateTo({
                            url: "/pages/webview/webview?h5=".concat(encodeURIComponent(t)),
                            success: function() {
                                o.setData({
                                    loading: !1
                                });
                            }
                        });
                    });
                }
            } else "MyReport" == t.target.dataset.id && wx.navigateTo({
                url: "/pages/authorize/authorize?path=report"
            }), "MyEInvoice" == t.target.dataset.id && wx.navigateTo({
                url: "/pages/authorize/authorize?path=eInvoice"
            });
        }
    }
});